package search

import (
	"fmt"
	"os"
)
import "github.com/spf13/cobra"

const version = "v7.7.7"
const author = "https://gitee.com/superwindcloud"

func ExecuteSearch()  {

	var rootCmd = &cobra.Command{
		Use:   "hyper_search",
		Short: "一个适用于go的包管理器, 类似cargo add 和 cargo search 的结合",
		Long:  "\x1b[95m 适用于go的包管理器, 类似cargo add 和 cargo search 的结合\x1b[0m\n",
		Run: func(cmd *cobra.Command, args []string) {
			if len(args) == 0 {
				err := cmd.Help()
				if err != nil {
					return
				}
			}
		},
		Version: version,
	}

	var searchCmd = &cobra.Command{
		Use:     "search",
		Short:   "搜索指定GO包",
		Long:    `搜索指定GO包`,
		Args:      cobra.MinimumNArgs(1),
		Example: "hyper_search  search  cobra",
		Aliases:  []string{"s"},
		Version: version,
		Run: func(cmd *cobra.Command, args []string) {
			if len(args) == 0 {
				err := cmd.Help()
				if err != nil {
					return
				}
				fmt.Print("\033[31m参数不足\033[0m\n")
			} else {
				err := displaySearchResult(args[0])
				if err != nil {
					fmt.Println(err)
					return
				}
			}
		},
	}
	var addCmd = &cobra.Command{
		Use:     "add",
		Short:   "添加指定依赖到go.mod文件",
		Version: version,
		Long:    "添加指定依赖到go.mod文件",
		Args:    cobra.MinimumNArgs(1),

		Example: "hyper_search  add   cobra   , hyper_search  add  cobra@v1.8.1"  ,
		Run: func(cmd *cobra.Command, args []string) {
			if len(args) == 0 {
				err := cmd.Help()
				if err != nil {
					return
				}
			} else {
				addDependencyToGoMod(args[0])
			}
		},
	}

	rootCmd.PersistentFlags().BoolP("version", "v", false, "显示应用程序的版本信息")
	rootCmd.SetVersionTemplate(fmt.Sprintf("App version: %s\n", version))
	rootCmd.Version = version
    searchCmd.SetVersionTemplate( fmt.Sprintf("App version: %s\n", version))
	searchCmd.Version = version
	addCmd.SetVersionTemplate(fmt.Sprintf("App version: %s\n", version))
	addCmd.Version = version
	rootCmd.SetHelpCommand(&cobra.Command{
		Use:    "no-help",
		Hidden: true,
	})
	completion := completionCommand()
	completion.Hidden = true


	rootCmd.PersistentFlags().StringP("author", "a", "https://gitee.com/superwindcloud", "显示作者信息")
	rootCmd.SetUsageTemplate(rootCmd.UsageTemplate() + "\nAuthor: " + author + "\n")

	rootCmd.AddCommand(searchCmd, addCmd, completion)

	err := rootCmd.Execute()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

}
func completionCommand() *cobra.Command {
	return &cobra.Command{
		Use:   "completion",
		Short: "Generate the autocompletion script for the specified shell",
	}
}
